﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using System.Web.Mvc;

namespace ScrumTracker.Model
{
    [MetadataType(typeof(Meta))]
    public partial class Story
    {
        public static Story Create(Story model)
        {
            //Mapper.CreateMap<Product.DTO, Product>();
            //Product newObject = Mapper.Map<Product>(model);
            model.CreatedAt = DateTime.UtcNow;
            ScrumTrackerEntities.Instance.Issues.AddObject(model);
            ScrumTrackerEntities.Instance.SaveChanges();
            return model;
        }

        public static Product Find(int id)
        {
            var product = ScrumTrackerEntities.Instance.Products.SingleOrDefault(p => p.ProductId.Equals(id));// .Where(p => p.IsHidden ?? true).OrderBy(p => p.Name).Skip(skip).Take(take);
            //Mapper.CreateMap<Product, T>();//.ForMember(d => d.CreatedAt, opt => opt.MapFrom(src => src.CreatedAt.ToShortDateString()));
            return product;
        }

        public static List<Product> Search(int skip = 0, int take = int.MaxValue, bool includeHidden = false)
        {
            var products = ScrumTrackerEntities.Instance.Products.Where(p => ((p.IsHidden ?? false) == false || p.IsHidden == includeHidden) && (p.IsDeleted ?? false) == false).OrderBy(p => p.Name).Skip(skip).Take(take);
            //Mapper.CreateMap<Product, T>();//.ForMember(d => d.CreatedAt, opt => opt.MapFrom(src => src.CreatedAt.ToShortDateString()));
            return products.ToList();
        }

        public static Product Add(Product.DTO model)
        {
            Mapper.CreateMap<Product.DTO, Product>();
            Product newObject = Mapper.Map<Product>(model);
            newObject.CreatedAt = DateTime.UtcNow;
            ScrumTrackerEntities.Instance.Products.AddObject(newObject);
            ScrumTrackerEntities.Instance.SaveChanges();
            return newObject;
        }

        public static bool Delete(int id)
        {
            var db = ScrumTrackerEntities.Instance;
            var product = new Product() { ProductId = id, IsDeleted = true };
            db.Products.Attach(product);
            db.ObjectStateManager.GetObjectStateEntry(product).SetModifiedProperty("IsDeleted");
            db.SaveChanges();
            return true;
        }

        [JsonObject]
        public class DTO
        {
            [Display(Name = "Product ID"), JsonProperty]
            public int ProductId { get; set; }
            [Required(AllowEmptyStrings = false), JsonProperty]
            public string Name { get; set; }
            [JsonProperty]
            public string DefinitionOfDone { get; set; }
            [JsonProperty]
            public bool IsHidden { get; set; }
            [JsonProperty]
            public DateTime CreatedAt { get; set; }
        }

        public class Meta
        {
            [Required, DataType(DataType.MultilineText)]
            public string Description { get; set; }
        }
    }

}
